package com.doge.calcalbum.business.vip

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.MutableLiveData
import com.doge.calcalbum.R
import com.doge.calcalbum.core.base.LoadStatus
import com.doge.calcalbum.utils.appContext
import com.doge.calcalbum.utils.dp
import com.doge.calcalbum.utils.log
import com.doge.calcalbum.utils.toast

object ViewBuyWebHelper {
    val loading = MutableLiveData<LoadStatus>()

    fun load(webView: WebView?, url: String) {
        loading.value = LoadStatus.LOADING
        webView?.loadUrl(url)
    }

    @SuppressLint("SetJavaScriptEnabled")
    fun attach(anchor: View): WebView {
        val webView = WebView(appContext)
        webView.layoutParams = ConstraintLayout.LayoutParams(1.dp, 1.dp)
        val webSettings = webView.settings
        webSettings.textZoom = 100
        webSettings.javaScriptEnabled = true
        webSettings.allowContentAccess = true
        webSettings.builtInZoomControls = false
        webSettings.useWideViewPort = true
        webSettings.loadWithOverviewMode = true
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE //设置缓存 // 解决js好像调用不了的问题 具体问 陈恩典
        webSettings.mixedContentMode =
            WebSettings.MIXED_CONTENT_ALWAYS_ALLOW // https://blog.csdn.net/zlgzh/article/details/70171746 https页面加载http图片
        webSettings.domStorageEnabled = true
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }
        webSettings.blockNetworkImage = false
        webSettings.javaScriptCanOpenWindowsAutomatically = true//支持js调用window.open方法
        webSettings.setSupportMultipleWindows(true)// 设置允许开启多窗口
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                log("ViewBuyWebHelper shouldOverrideUrlLoading $url")
                return if (url.startsWith("weixin:") ||
                    url.startsWith("alipays:")
                ) {
                    // weixin://wap/pay
                    // alipays://platformapi/startApp
                    loading.value = LoadStatus.SUCCESS
                    try {
                        anchor.context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
                    } catch (e: Throwable) {
                        // android.content.ActivityNotFoundException: No Activity found to handle Intent
                        if (url.startsWith("weixin:")) {
                            toast(R.string.not_install_wechat)
                        } else if (url.startsWith("alipays:")) {
                            toast(R.string.not_install_alipay)
                        }
                    }
                    true
                } else false
            }
        }
        return webView
    }
}